package com.ruoyi.common.utils.phone;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by mai xiaogang on 2018/11/19.
 * 手机号格式效验
 */
public class PhoneCheckUtil {
    /**
     * 国内手机号校验
     * 移动号段：139 138 137 136 135 134 147 150 151 152 157 158 159 178 182 183 184 187 188 198
     * 联通号段：130 131 132 155 156 166 185 186 145 176
     * 电信号段：133 153 177 173 180 181 189 199
     * 虚拟运营商号段：170 171
     */
    public static boolean phoneCheck(String phone) {
        if (null == phone || "".equals(phone)) {
            return false;
        } else {
            if (phone.length() != 11) {
                return false;
            }
            String regex = "^1[23456789]\\d{9}$";
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            boolean isMatch = m.matches();
            if (isMatch) {
                return true;
            }
        }
        return false;
    }

    public static boolean foreigPhoneCheck(String phone) {
        if (null == phone || "".equals(phone)) {
            return false;
        } else {
            if (phone.length() < 1 || phone.length() > 15) {
                return false;
            }
            String regex = "^[0-9]\\d*$";
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            boolean isMatch = m.matches();
            if (isMatch) {
                return true;
            }
        }
        return false;
    }

    public static boolean phoneTypeCheck(String phone, String type) {
        //国内校验
        if ("86".equals(type)) {
            return phoneCheck(phone);
        } else {
            return foreigPhoneCheck(phone);
        }
    }


    //邮箱校验
    public static boolean emailCheck(String email) {
        String regex = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(email);
        boolean isMatch = m.matches();
        if (isMatch) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        boolean flag = PhoneCheckUtil.phoneTypeCheck("755-86285739", "81");
        System.out.println(flag);
    }
}
